package com.erp.wallet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.wallet.domain.ErpSellerStoreWalletOrder;
import com.erp.wallet.domain.bo.ErpSellerStoreWalletOrderBo;
import com.erp.wallet.domain.vo.ErpSellerStoreWalletOrderVo;
import com.erp.wallet.mapper.ErpSellerStoreWalletOrderMapper;
import com.erp.wallet.service.IErpSellerStoreWalletOrderService;
import com.erp.wallet.service.IErpSellerStoreWalletService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.ExpenseType;
import com.ruoyi.common.core.enums.FlowType;
import com.ruoyi.common.core.enums.PayType;
import com.ruoyi.common.core.pay.AlipayUtils;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 钱包订单Service业务层处理
 *
 * @author erp
 * @date 2023-05-25
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreWalletOrderServiceImpl implements IErpSellerStoreWalletOrderService {

	private final ErpSellerStoreWalletOrderMapper baseMapper;
	private final IErpSellerStoreWalletService iErpSellerStoreWalletService;

	/**
	 * 查询钱包订单
	 */
	@Override
	public ErpSellerStoreWalletOrderVo queryById(Long id) {
		return baseMapper.selectVoById(id);
	}

	/**
	 * 查询钱包订单列表
	 */
	@Override
	public TableDataInfo<ErpSellerStoreWalletOrderVo> queryPageList(ErpSellerStoreWalletOrderBo bo,
                                                                    PageQuery pageQuery) {
		LambdaQueryWrapper<ErpSellerStoreWalletOrder> lqw = buildQueryWrapper(bo);
		Page<ErpSellerStoreWalletOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询钱包订单列表
	 */
	@Override
	public List<ErpSellerStoreWalletOrderVo> queryList(ErpSellerStoreWalletOrderBo bo) {
		LambdaQueryWrapper<ErpSellerStoreWalletOrder> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<ErpSellerStoreWalletOrder> buildQueryWrapper(ErpSellerStoreWalletOrderBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ErpSellerStoreWalletOrder> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getType() != null, ErpSellerStoreWalletOrder::getType, bo.getType());
		lqw.eq(bo.getPayType() != null, ErpSellerStoreWalletOrder::getPayType, bo.getPayType());
		lqw.eq(StringUtils.isNotBlank(bo.getOrderNumber()), ErpSellerStoreWalletOrder::getOrderNumber,
				bo.getOrderNumber());
		lqw.eq(bo.getMoney() != null, ErpSellerStoreWalletOrder::getMoney, bo.getMoney());
		lqw.eq(bo.getAddTime() != null, ErpSellerStoreWalletOrder::getAddTime, bo.getAddTime());
		return lqw;
	}

	/**
	 * 新增钱包订单
	 */
	@Override
	public R<Object> insertByBo(ErpSellerStoreWalletOrderBo bo) {
		ErpSellerStoreWalletOrder add = BeanUtil.toBean(bo, ErpSellerStoreWalletOrder.class);
		validEntityBeforeSave(add);

		// 生成订单号
		String orderNumber = "WA" + System.currentTimeMillis();
		add.setOrderNumber(orderNumber);
		add.setAddTime(new Date());
		add.setStatus(0); // 未支付

		baseMapper.insert(add);

		// 调用支付宝工具类
		String webpagePay = AlipayUtils.webpagePay("ERP充值", orderNumber, add.getMoney().doubleValue());

		return R.ok(webpagePay,"");
	}

	/**
	 * 修改钱包订单
	 */
	@Override
	public Boolean updateByBo(ErpSellerStoreWalletOrderBo bo) {
		ErpSellerStoreWalletOrder update = BeanUtil.toBean(bo, ErpSellerStoreWalletOrder.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(ErpSellerStoreWalletOrder entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除钱包订单
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}

	/**
	 * 
	 * @param orderNumber 商户订单号
	 * @param tradeNo     第三方订单号
	 */
	@Override
	public void callback(String orderNumber, String tradeNo) {
		// 根据订单号获取记录
		LambdaQueryWrapper<ErpSellerStoreWalletOrder> lqw = new LambdaQueryWrapper<ErpSellerStoreWalletOrder>();
		lqw.eq(ErpSellerStoreWalletOrder::getOrderNumber, orderNumber);
		lqw.last("limit 1");
		ErpSellerStoreWalletOrder order = baseMapper.selectOne(lqw);
		order.setStatus(1); // 已支付
		order.setTradeNo(tradeNo); // 第三方支付单号
		baseMapper.updateById(order);

		// 修改钱包金额
		iErpSellerStoreWalletService.updateWallet(FlowType.INCOME.getCode(), ExpenseType.RECHARGE.getCode(),
				PayType.ZFB.getCode(), order.getMoney(), Long.valueOf(order.getCreateBy()), orderNumber, "");
	}
}
